package com.cema.manage.project.task;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.project.enums.RedisEnum;
import com.cema.manage.project.enums.UserStatusEnum;
import com.cema.manage.project.manage.adRecord.domain.AdRecord;
import com.cema.manage.project.manage.adRecord.service.IAdRecordService;
import com.cema.manage.project.manage.antiCheatDetail.domain.AntiCheatDetail;
import com.cema.manage.project.manage.antiCheatDetail.service.IAntiCheatDetailService;
import com.cema.manage.project.manage.users.domain.Users;
import com.cema.manage.project.manage.users.service.IUsersService;
import com.cema.manage.project.util.DateUtils;
import com.cema.manage.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 价值过低封号
 *
 * @author Administrator
 */
@Service("riskTask")
public class RiskTask {
    private static final Logger log = LoggerFactory.getLogger(RiskTask.class);
    @Resource
    private IAdRecordService iAdRecordService;

    @Resource
    private IAntiCheatDetailService iAntiCheatDetailService;

    @Resource
    private IUsersService iUsersService;

    @Resource
    private RedisService redisService;

    private List<AntiCheatDetail> list;
    private List<Users> usersList;

    public void init() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        if (!(hour == 23 && minute == 59)) {
            return;
        }
        log.info("贡献值过低任务启动 start");
        //时间验证
        EntityWrapper<AdRecord> entityWrapper = new EntityWrapper<>();
        DateUtils.getCurrentDayTime(entityWrapper, "create_time");
        List<AdRecord> adRecordList = iAdRecordService.selectList(entityWrapper);
        if (ObjectUtil.isNull(adRecordList) || adRecordList.size() == 0) {
            log.info("贡献值过低任务启动 end return1");
            return;
        }
        String consThanEqual = (String) redisService.get(RedisEnum.RISK_ADVERTISING_COINS_THAN_EQUAL.getKey());
        if (StrUtil.isEmpty(consThanEqual)) {
            log.info("贡献值过低任务启动 end return2");
            return;
        }
        list = new ArrayList<>();
        usersList = new ArrayList<>();
        List<AntiCheatDetail> antiCheatDetails = iAntiCheatDetailService.selectList(new EntityWrapper<>());
        String[] strings = consThanEqual.split(",");
        Map<Integer, List<AdRecord>> adRecordMap = adRecordList.stream().collect(Collectors.groupingBy(AdRecord::getUsId));
        adRecordMap.keySet().stream().forEach(key -> {
            List<AdRecord> adRecords = adRecordMap.get(key);
            int number = Integer.parseInt(strings[0]);
            if (adRecords.size() >= number) {
                int coins = Integer.parseInt(strings[1]);
                int sum = 0;
                for (int i = 0; i < number; i++) {
                    AdRecord record = adRecords.get(i);
                    sum = sum + record.getArRealityMoney();
                }
                boolean flag;
                if (ObjectUtil.isNull(antiCheatDetails) || antiCheatDetails.size() == 0) {
                    flag = true;
                } else {
                    long count = antiCheatDetails.stream().filter(antiCheatDetail -> key.equals(antiCheatDetail.getUsId())).count();
                    flag = count <= 0;
                }
                if (coins >= sum / number && flag) {
                    AntiCheatDetail antiCheatDetail = new AntiCheatDetail();
                    Users users = new Users();
                    antiCheatDetail.setUsId(key);
                    users.setId(key);
                    users.setStatus(UserStatusEnum.USER_STATUS_VIOLATION.getValue());
                    antiCheatDetail.setAcdDesc("贡献值过低");
                    list.add(antiCheatDetail);
                    usersList.add(users);
                }
            }
        });
        if (ObjectUtil.isNull(list) || list.size() == 0) {
            log.info("贡献值过低任务启动 end return3");
            return;
        }
        iAntiCheatDetailService.insertBatch(list);
        iUsersService.updateBatchById(usersList);
        log.info("贡献值过低任务启动 end return");
    }
}
